package com.example.hans.retrofitrxjavaokhttp;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {

    private List<Movie> mMovieList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView mImageView;
        TextView mTextView;

        public ViewHolder(View view) {
            super(view);
            mImageView = (ImageView) view.findViewById(R.id.movie_photo);
            mTextView = view.findViewById(R.id.movie_info);

        }
    }

    public void setMovieList(List<Movie> mMovieList) {
        this.mMovieList = mMovieList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.movie_item,viewGroup,false);

        ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int i) {

        Movie movie = mMovieList.get(i);

        ImageLoader.getInstance().displayImage(movie.getImgUrl(),holder.mImageView);
        holder.mTextView.setText(movie.getChineseName()+"\n"
        + movie.getEnglishName()+"\n"
        + movie.getReleaseDate());

    }

    @Override
    public int getItemCount() {
        return mMovieList == null?0:mMovieList.size();
    }
}
